package com.photoeditor.demo.ui.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.photoeditor.R;
import com.photoeditor.demo.imageloader.ImageLoader;
import com.photoeditor.demo.ui.widget.image.TiledBitmapView;
import com.photoeditor.demo.util.MediaTypeUtil;

import me.xiaopan.sketch.SketchImageView;

/**
 * 通过不同类型显示的view，支持类型有图片、视频、gif
 * Created by mutour on 2017/10/26.
 */

public class MediaView extends FrameLayout {
    private Context mContext;
    private View mView;

    public MediaView(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public MediaView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    public MediaView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MediaView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
    }


    public void destroy() {
        if (mView instanceof TextureVideoView) {
            ((TextureVideoView) mView).stop();
        } else if (mView instanceof TiledBitmapView) {
            ((TiledBitmapView) mView).recycle();
        }
    }

    public void onStart() {
        if (mView instanceof TextureVideoView) {
            ((TextureVideoView) mView).resume();
        }
    }

    public void onStop() {
        if (mView instanceof TextureVideoView) {
            ((TextureVideoView) mView).pause();
        }
    }

    /**
     * @param src
     * @param type         {@link MediaTypeUtil}
     * @param layoutParams
     */
    public void setMedia(String src, int type, FrameLayout.LayoutParams layoutParams) {
        this.removeAllViews();
        if (layoutParams == null) {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.gravity = Gravity.CENTER;
            layoutParams = lp;
        }

        View view = null;
        if (MediaTypeUtil.isImage(type)) {
            if (MediaTypeUtil.isGif(type)) {
                view = newGifMedia(src);
            } else {
                view = newImageMedia(type, src);
            }
        } else if (MediaTypeUtil.isVideo(type)) {
            view = newVideoMedia(src);
        } else if (MediaTypeUtil.isText(type)) {
            view = newTextMedia(src);
        }
        if (view != null) {
            mView = view;
            this.addView(view, layoutParams);
        }
    }

    private View newTextMedia(String src) {
        TextView textView = new TextView(getContext());
        textView.setText(src);
        return textView;
    }

    private View newGifMedia(String src) {
        SketchImageView imageView = new SketchImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.getOptions().setDecodeGifImage(true);
        imageView.displayImage(src);
        return imageView;
    }

    private View newImageMedia(int type, String src) {
        ImageView imageView = new ImageView(mContext);
        final ImageView.ScaleType scaleType = ImageView.ScaleType.CENTER_CROP;
        imageView.setScaleType(scaleType);
        ImageLoader.loadImage(src, R.drawable.ic_default_image, imageView);
        return imageView;
    }

    private View newVideoMedia(final String src) {
        TextureVideoView view = new TextureVideoView(getContext());
        view.setAutoAdjustSize(false);
        view.setVideoFilePath(src);
        view.setPlayLooping(true);
        return view;
    }
}
